/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2023
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
const fwutil = {
  formatTime: function(date) {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      const hour = date.getHours()
      const minute = date.getMinutes()
      const second = date.getSeconds()

      return `${[year, month, day].map(this.formatNumber).join('-')} ${[hour, minute, second].map(this.formatNumber).join(':')}`
  },
  formatDate: function(date) {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      return [year, month,day].map(this.formatNumber).join('-')
  },
  formatNumber: function(n) {
      n = n.toString();
      return n[1] ? n : `0${n}`;
  },
  /*
    计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
    默认单位m
    */
   calcMapDistance:function(lat1,lng1,lat2,lng2){
    var radLat1 = this.calcMapRadius(lat1);
    var radLat2 = this.calcMapRadius(lat2);
    var a = radLat1 - radLat2;
    var  b = this.calcMapRadius(lng1) - this.calcMapRadius(lng2);
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
    s = s *6378.137 ;// EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000; //输出为公里
    //s=s.toFixed(2);
    return s*1000;
  },
  //进行经纬度转换为距离的计算
  calcMapRadius:function(d){
    return d * Math.PI / 180.0;//经纬度转换成三角函数中度分表形式。
  }
}
module.exports = fwutil;
